import pygame, sys
from pygame.time import *
from pygame.locals import *
from pygame import *

from Constants import *
from Minion import Minion
from Team import *
from Map import *

class Game:
    def __init__(self):
        self.window = pygame.display.set_mode((RESOLUTION_WIDTH, RESOLUTION_HEIGHT))
        #pygame.mouse.set_visible(False)
        pygame.display.set_caption('TestCurrent')
        
        self.screen = pygame.display.get_surface()
        
        self.background = pygame.Surface(self.screen.get_size()).convert()
        self.backgroundColour = (50,50,50)
        self.background.fill(self.backgroundColour)

        self.keyHandler = {K_ESCAPE : lambda : sys.exit(0)}
        self.keyHandler[K_SPACE] = lambda : self.startSim()
        
        self.teams = []
        team1 = Team()
        team2 = Team((255,0,0))
        self.teams.append(team1)
        self.teams.append(team2)
        self.minions = []
        #self.minions.append(Minion())
        self.minions.append(Minion(team1, [200,200], [0,1], [0,1]))
        self.minions.append(Minion(team2, [500,200], [0,1], [0,1]))
        
        self.clock = Clock()
        self.clock.tick() #Set the clock
        self.msSincePhaseStart = 0
        
        self.running = False
        
        self.selectedMinion = None
        
        self.map = Map()

    
    def mainLoop(self):
        while True:
            #self.unrender()      
            self.update()
            self.render()
            self.input(pygame.event.get())
    
    def unrender(self):
        for minion in self.minions:
            minion.unrender(self.screen, self.backgroundColour)
        #pygame.display.flip()
    
    def render(self):
        #Think about replacing the following line with unrender
        self.screen.blit(self.background, (0,0))
        
        self.map.render(self.screen)
        
        for minion in self.minions:
            minion.render(self.screen)
        
        pygame.display.flip()
        
    
    def update(self):
        if self.running:
            msLapsed = self.clock.get_time()
            self.clock.tick(MAX_FPS)
            for minion in self.minions:
                minion.update(msLapsed)
            if msLapsed < SIMULATION_PHASE_SECONDS * 1000:
                self.msSincePhaseStart = self.msSincePhaseStart + msLapsed
            print self.msSincePhaseStart
            if self.msSincePhaseStart/1000 >= SIMULATION_PHASE_SECONDS:
                self.endSim()
            
    
    def input(self,events):
        for event in events:
            if event.type == QUIT:
                print 'Goodbye!'
                sys.exit(0)
            elif event.type == KEYDOWN:
                try:
                    self.keyHandler[event.key]()
                    self.render()
                except KeyError:
                    print 'key not bound'
            elif event.type == MOUSEMOTION:
                pass
            elif event.type == MOUSEBUTTONDOWN:
                print "Button down"
                mouse1, mouse2, mouse3 = pygame.mouse.get_pressed()
                if not self.running:
                    if mouse3 == True:
                        if self.selectedMinion <> None:
                            self.selectedMinion.unselect()
                            self.selectedMinion = None
                    elif mouse1 == True:
                        if self.selectedMinion == None:                 
                            for minion in self.minions:
                                if minion.minionAt(pygame.mouse.get_pos()):
                                    self.selectedMinion = minion
                            if self.selectedMinion <> None:
                                self.selectedMinion.select()
                        else:
                            self.selectedMinion.addWaypoint(pygame.mouse.get_pos())
            elif event.type == MOUSEBUTTONUP:
                print "Button up"
    
    def startSim(self):
        if not self.running:
            self.running = True
            self.clock.tick(MAX_FPS)
            self.clock.tick(MAX_FPS)
            self.msSincePhaseStart = 0
    
    def endSim(self):
        self.running = False           
                
game = Game().mainLoop()